package com.starbaba.core.service.impl;

import com.base.common.exception.AppException;
import com.base.common.log.LogService;
import com.google.common.collect.Lists;
import com.starbaba.base.dao.LoanManagerBaseDao;
import com.starbaba.common.model.ChannelGroup;
import com.starbaba.common.model.ParentProduct;
import com.starbaba.common.model.SecondaryChannelGroup;
import com.starbaba.core.service.CommonConditionService;
import com.tuanzi.server.loan_base.model.db.dictionary.LoanDictionary;
import com.tuanzi.server.loan_base.util.dictionary.LoanDictionaryUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenweijun
 * @Date: 2018/11/26 15:20
 * @Description:
 */
public class CommonConditionServiceImpl extends LoanManagerBaseDao implements CommonConditionService {

    @Override
    public List<ParentProduct> getParentProductList() {
        List<LoanDictionary> loanDictionaries = LoanDictionaryUtils.findAllByCode("LARGE_COMMON_PRODUCT");
        List<ParentProduct> parentProducts = Lists.newArrayList();

        for(LoanDictionary loanDictionary: loanDictionaries){
            ParentProduct parentProduct = new ParentProduct();
            parentProduct.setParentProductId(loanDictionary.getValue1());
            parentProduct.setParentProductName(loanDictionary.getName());

            parentProducts.add(parentProduct);
        }

        return parentProducts;
    }

    @Override
    public Map<String, String> getParentProductMap() {

        List<LoanDictionary> loanDictionaries = LoanDictionaryUtils.findAllByCode("LARGE_COMMON_PRODUCT");

        Map<String, String> map = new HashMap<>();
        for(LoanDictionary loanDictionary: loanDictionaries){
            map.put(loanDictionary.getValue1(), loanDictionary.getName());
        }

        return map;
    }

    @Override
    public List<ChannelGroup> getChannelGroupList() {
        List<LoanDictionary> loanDictionaries = LoanDictionaryUtils.findAllByCode("CHANNEL_GROUP");
        List<ChannelGroup> channelGroups = Lists.newArrayList();

        for(LoanDictionary loanDictionary: loanDictionaries){
            ChannelGroup channelGroup = new ChannelGroup();
            channelGroup.setChannelGroupId(loanDictionary.getValue1());
            channelGroup.setChannelGroupName(loanDictionary.getName());

            channelGroups.add(channelGroup);
        }

        return channelGroups;
    }

    @Override
    public List<SecondaryChannelGroup> getSecondaryChannelGroupList() {
        String sql = "SELECT id,channel_name AS channelName FROM secondary_channel_group";
        List<SecondaryChannelGroup> list = new ArrayList<>();
        try {
            list = getDao().list(sql, SecondaryChannelGroup.class);
        } catch (AppException e) {
            LogService.error(e);
        }
        return list;
    }

    @Override
    public Map<String, String> getChannelGroupMap() {
        List<LoanDictionary> loanDictionaries = LoanDictionaryUtils.findAllByCode("CHANNEL_GROUP");
        Map<String, String> map = new HashMap<>();
        for(LoanDictionary loanDictionary: loanDictionaries){
            map.put(loanDictionary.getValue1(), loanDictionary.getName());
        }
        return map;
    }

    @Override
    public Map<String, String> getSecondaryChannelGroupMap() {
        List<SecondaryChannelGroup> list = getSecondaryChannelGroupList();
        Map<String, String> map = new HashMap<>();
        for(SecondaryChannelGroup group: list){
            map.put(group.getId().toString(), group.getChannelName());
        }
        return map;
    }
}
